using System.Runtime.CompilerServices;
#if MYCODE
using BepuUtilities.Vectors;
#else
using System.Numerics;
#endif
namespace BepuUtilities
{
    /// <summary>
    /// 一些用于索引到向量束的帮助器。
    /// </summary>
    public static class BundleIndexing
    {
        /// <summary>
        /// <！[CDATA[获取掩码值,以便x&VectorMask计算x%Vector<Float>.Count.]]>
        /// </summary>
        /// <remarks>The JIT recognizes that this value is constant!</remarks>
        public static int VectorMask
        {
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            get
            {
                return Vector<float>.Count - 1;
            }
        }
        /// <summary>
        /// <！[CDATA[获取移位值,以便x>>VectorShift将x除以向量<Float>.Count.]]>
        /// </summary>
        /// <remarks>The JIT recognizes that this value is constant!</remarks>
        public static int VectorShift
        {
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            get
            {
                switch (Vector<float>.Count)
                {
                    case 4:
                        return 2;
                    case 8:
                        return 3;
                    case 16:
                        return 4;
                    default:
                        return 0;
                }
            }
        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static void GetBundleIndices(int linearIndex, out int bundleIndex, out int indexInBundle)
        {
            bundleIndex = linearIndex >> VectorShift;
            indexInBundle = linearIndex & VectorMask;
        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static int GetBundleCount(int elementCount)
        {
            return (elementCount + VectorMask) >> VectorShift;
        }
    }
}
